<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Org\Util\Easemob;

class StoreController extends CommonController {

	//店铺首页
	public function index(){
		$area=I('area');
		$shopname = I('shopname');
		$phone = I('phone');
		$shop_type = I('shop_type');
		
		$qu=explode('/',$area);

		if (!empty($area)) {
			$where = array("area"=>$qu['2']);
            $where['pro'] = $qu['0'];
            $where['city'] = $qu['1'];
			//P($qu['2']);
			$this->assign('areaArr',$area);
		}
		if($shop_type){
			$where['shop_type'] = $shop_type;
		}

		if (!empty($shopname)) {
			$where['shopname'] = array("like","%".$shopname."%");
			$this->assign('shopname',$shopname);
		}

		if (!empty($phone)) {
			$where ['phone|tel']= array("like",'%'.$phone.'%');
			$this->assign('phone',$phone);
		}

		$Count=M('shop')->where($where)->where('status=1 or status =3')->count();
		 
		$Page = new \Think\Pager($Count,10);
		$show=$Page->showhtml(); 
		$list=M('shop')->where($where)->where('status=1 or status =3')->limit($Page->firstRow.','.$Page->listRows)->order("sort desc")->select();
	 
		$types = M('shop_type')->order('sort desc')->getField('id,type_name');
		if($list){
			foreach ($list as $key => &$row) {
				$row['shop_type'] = $row['shop_type'] ? isset($types[$row['shop_type']]) ? $types[$row['shop_type']] :'' : '';
			}
		}

		$this->assign('shop_type',$shop_type);
		$this->assign('types', $types);
		$this->assign('page',$show);
		$this->assign('list',$list);
		$this->display();
	}

	//关闭店铺
	public function close(){
		$where['id']=I('id');
		$data=array('status'=>3);
		$res=M('shop')->where($where)->save($data);
		if($res){
			$this->ajaxReturn(array('s'=>true,'m'=>'店铺已关闭'));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>'操作失败'));
		}
	}

	//开启
	public function open(){
		$where['id']=I('id');
		$data=array('status'=>1);
		$res=M('shop')->where($where)->save($data);
		if($res){
			$this->ajaxReturn(array('s'=>true,'m'=>'店铺已开启'));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>'操作失败'));
		}
	}

	public function show() {
		$id = I('id');
		$shopInfo = M("shop") -> where(array("id" => $id)) -> find();
		$shopInfo['showimg']=explode(',',$shopInfo['showimg']);
		$shopInfo['yyzz']=explode(',',$shopInfo['yyzz']);
		$shopInfo['boss_img']=explode(',',$shopInfo['boss_img']);
		//P($shopInfo);
		$this->assign('list', $shopInfo);
		$this->display();
	}
	//添加店铺
	public function add(){

		$advertising = M('Article_advertising')->select();
		$types = M('shop_type')->where(['status'=>1])->order('sort desc')->select();
		$nearby_shops = M('nearby_shop')->where(['status'=>1])->order('id desc')->field('id,shop_name,name,mobile,province,city,district')->select();
		foreach ($nearby_shops as $key => &$nearby_shop) {
			$nearby_shop['shop_name'] = $nearby_shop['shop_name'].'-'.$nearby_shop['province'].'-'.$nearby_shop['city'].'-'.$nearby_shop['district'];
		}
		$this->assign('advertising', $advertising);
		$this->assign('types', $types);
		$this->assign('nearby_shops', $nearby_shops);
		$this->display();
	}
	//验证经销商电话
	public function yz(){
		$phone=I('phone');
		$res=M('dealer')->where(array('phone'=>$phone))->find();
		if($res){
			$this->ajaxReturn(array('s'=>true,'d'=>$res['id']));
		}else{
			$this->ajaxReturn(array('s'=>false));
		}
		
	}
	//验证手机是否注册
	public function user(){
		$phone=I('phone');
		$res=M('user')->where(array('phone'=>$phone))->find();
		if(!$res){
			$this->ajaxReturn(array('s'=>true));
		}else{
			$this->ajaxReturn(array('s'=>false));
		}
		
	}

	public function doadd(){
	
		$uid=I('uid');
		$name=I('name');
		$shopname=I('shopname');
		$ds=I('ds');
		$ds = str_replace("\n","<br />",$ds);
		$addr=I('addr');
		$phone=I('phone');
		$shop_type=I('shop_type');
		$shop_type = $shop_type ? $shop_type : 0;
		$nearby_shop_id=I('nearby_shop_id');
		$nearby_shop_id = $nearby_shop_id ? $nearby_shop_id : 0;
		$did=I('did');
		$tel=I('tel');
		$card=I('card');
		$listimg=I('listimg');
		$show = I('showimg');
		$showimg = $show;	//编码
		$pca = I('pca');
		$areaArr=explode('/',$pca);
		$pro = $areaArr[0];
		$city = $areaArr[1];
		$area = $areaArr[2];
		$lng = I('lng');
		$lat = I('lat');
		$ad_id = I('ad_id');
		$video = I('video');
		$systems = I('systems');
		if(!empty($phone)){
			$userimg	=	C('SiteUrl').'Public/upload/userimg/t.jpg';
			//添加用户信息
			$data1 = array(
				'img'=>$userimg,
				'nickname'=>"用户".$phone,
				'phone'=>$phone,
				'pwd'=>md5(123456),
				'ctime'=>time(),
			);
            $userInfo = user_reg($data1);
            if(!$userInfo){
                $this->ajaxReturn(array('s'=>false,'m'=>'用户添加失败!'));
            }
			
			$conf = C('options');
			$Easemob = new Easemob($conf); 
			$Easemob->createUser($phone,'12345');
			$Easemob->editNickname($phone,$phone);
			$data = array(
				'uid'=>$userInfo,
				'name'=>$name,
				'shop_type'=>$shop_type,
				'nearby_shop_id'=>$nearby_shop_id,
				'shopname'=>$shopname,
				'ds'=>$ds,
				'pro'=>$pro,
				'city'=>$city,
				'area'=>$area,
				'addr'=>$addr,
				'phone'=>$phone,
				'did'=>$did,
				'tel'=>$tel,
				'card'=>$card,
				'lng'=>$lng,
				'lat'=>$lat,
				'ad_id'	=> $ad_id,
				'video'	=> $video,
				'listimg'=>$listimg,
				'showimg'=>$showimg,
				'status'=>1,
				'ctime'=>time(),
				'systems'=>$systems,
			);	
		}else{
			$data = array(
				'name'=>$name,
				'shop_type'=>$shop_type,
				'nearby_shop_id'=>$nearby_shop_id,
				'shopname'=>$shopname,
				'ds'=>$ds,
				'pro'=>$pro,
				'city'=>$city,
				'area'=>$area,
				'addr'=>$addr,
				'did'=>$did,
				'tel'=>$tel,
				'card'=>$card,
				'lng'=>$lng,
				'lat'=>$lat,
				'ad_id'	=> $ad_id,
				'video'	=> $video,
				'listimg'=>$listimg,
				'showimg'=>$showimg,
				'status'=>1,
				'ctime'=>time(),
				'systems'=>$systems,
			);	
		}
		
		
		$shop_list=M('shop')->data($data)->add();

		if($shop_list){
			$this->ajaxReturn(array('s'=>true,'m'=>'添加成功!'));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"添加失败!"));
		}
	}

	//修改
	public function update(){
		$id=I('id');

		$shopInfo=M('shop')->find($id);

		
		$shopInfo['showimg'] = explode(",",$shopInfo['showimg']);
		$shopInfo['ds'] = str_replace("<br />","\n",$shopInfo['ds']);
		$where['id']=$shopInfo['did'];
		
		$dphone=M('dealer')->where($where)->find();
		$shopInfo['dphone']=$dphone['phone'];

		$advertising = M('Article_advertising')->select();
		$types = M('shop_type')->where(['status'=>1])->order('sort desc')->select();

		$nearby_shops = M('nearby_shop')->where(['status'=>1])->order('id desc')->field('id,shop_name,name,mobile,province,city,district')->select();
		foreach ($nearby_shops as $key => &$nearby_shop) {
			$nearby_shop['shop_name'] = $nearby_shop['shop_name'].'-'.$nearby_shop['province'].'-'.$nearby_shop['city'].'-'.$nearby_shop['district'];
		}
		
		$this->assign('advertising', $advertising);
		$this->assign('list',$shopInfo);
		$this->assign('types', $types);
		$this->assign('nearby_shops', $nearby_shops);
		$this->display();
	}
	//修改操作
	public function doupdate(){
		$id=I('id');
		$name=I('name');
		$shopname=I('shopname');
		$ds=I('ds');
		$ds = str_replace("\n","<br />",$ds);
		$addr=I('addr');
		$tel=I('tel');
		$did=I('did');
		$phone=I('phone');
		$shop_type=I('shop_type');
		$shop_type = $shop_type ? $shop_type : 0;
		$nearby_shop_id=I('nearby_shop_id');
		$nearby_shop_id = $nearby_shop_id ? $nearby_shop_id : 0;
		$card=I('card');
		$video = I('video');
		$listimg=I('listimg');
		$show = I('showimg');
		$showimg = $show;	//编码
		$pca = I('pca');
		$areaArr=explode('/',$pca);
		$pro = $areaArr[0];
		$city = $areaArr[1];
		$area = $areaArr[2];
		$ad_id = I('ad_id');
		$lng = I('lng');
		$lat = I('lat');
		$systems = I('systems');
		$is_discount_today = I('is_discount_today');
		
		if(!empty($phone)){
			$userimg	=	C('SiteUrl').'Public/upload/userimg/t.jpg';
			//添加用户信息
			$data1 = array(
				'img'=>$userimg,
				'nickname'=>"用户".$phone,
				'phone'=>$phone,
				'pwd'=>md5(123456),
				'ctime'=>time(),
			);
            $userInfo = user_reg($data1);
            if(!$userInfo){
                $this->ajaxReturn(array('s'=>false,'m'=>'用户添加失败!'));
            }
			
			$conf = C('options');
			$Easemob = new Easemob($conf); 
			$Easemob->createUser($phone,'12345');
			$Easemob->editNickname($phone,$phone);
				$data = array(
					'uid'=>$userInfo,
					'phone'=>$phone,
					'shop_type'=>$shop_type,
					'nearby_shop_id'=>$nearby_shop_id,
					'name'=>$name,
					'shopname'=>$shopname,
					'ds'=>$ds,
					'pro'=>$pro,
					'city'=>$city,
					'area'=>$area,
					'addr'=>$addr,
					'tel'=>$tel,
					'did'=>$did,
					'card'=>$card,
					'lng'=>$lng,
					'lat'=>$lat,
					'video'=>$video,
					'listimg'=>$listimg,
					'showimg'=>$showimg,
					'is_discount_today'=>$is_discount_today,
					'ad_id'	=> $ad_id,
					'systems'=>$systems,
				);
		}else{
			$data = array(
			'name'=>$name,
			'shop_type'=>$shop_type,
			'nearby_shop_id'=>$nearby_shop_id,
			'shopname'=>$shopname,
			'ds'=>$ds,
			'pro'=>$pro,
			'city'=>$city,
			'area'=>$area,
			'addr'=>$addr,
			'tel'=>$tel,
			'did'=>$did,
			'card'=>$card,
			'lng'=>$lng,
			'lat'=>$lat,
			'video'=>$video,
			'listimg'=>$listimg,
			'showimg'=>$showimg,
			'is_discount_today'=>$is_discount_today,
			'ad_id'	=> $ad_id,
			'systems'=>$systems,
			);
		}

		try{
			$shop_list=M('shop')->where(array('id'=>$id))->data($data)->save();
			//echo M()->_sql();exit;
		}catch( \Exception $e){
				$this->ajaxReturn(array('s'=>false,'m'=>"修改失败!"));
		}	
		$this->ajaxReturn(array('s'=>true,'m'=>'修改成功!'));

		
	}



	//删除店铺信息
	public function del(){
		$id=I('id');

		$shop=M('shop')->find($id);

		$shopInfo=M('shop')->where(array('id'=>$id))->delete();
		
		// try {
		// 	//删除用户
		// 	$userInfo = M('user')->where(array('id'=>$shop['uid']))->delete();
	
		// 	//删除地址
		// 	$addrInfo = M('addr')->where(array('uid'=>$shop['uid']))->delete();
			
		// } catch(Exception $e) {
		// 	$this -> ajaxReturn(array('s' => false, 'm' => '参数错误sql'));
		// }
		
		

		if($shopInfo ){

			$this->ajaxReturn(array("s"=>true,"m"=>"删除成功!"));
		}else{
			$this->ajaxReturn(array("s"=>false,"m"=>"删除失败!"));
		}
		
	}


	//跟据区置顶店铺
	public function topInfo(){
		$id=trim(I("id"));
		$sort = I("sort");
		$area = I("area");
		
		//根据区域查是否有三个置顶
		$where =array("area"=>$area,);

		$shopList=M("shop")->where($where)->select();

		foreach ($shopList as $key => $value) {
			if($sort == $value['sort']){
				$this->ajaxReturn(array("s"=>false,"m"=>"已有三个置顶,置顶失败!请先取消置顶!"));
			}

		}

		$data = array(
			'id'=>$id,
			"sort"=>$sort,
		);
		$list=M("shop")->data($data)->save();
		
		if($list){
			$this->ajaxReturn(array("s"=>true,"m"=>"置顶成功!"));
		}else{
			$this->ajaxReturn(array("s"=>false,"m"=>"置顶失败!"));
		}
	}

	public function delTopInfp(){
		$id=I("id");
		
		$data = array(
			'id'=>$id,
			"sort"=>0,
		);
		$shopInfo=M("shop")->data($data)->save();

		if($shopInfo){
			$this->ajaxReturn(array("s"=>true,"m"=>"取消成功!"));
		}else{
			$this->ajaxReturn(array("s"=>false,"m"=>"取消失败!"));
		}
	}

	public function pinluan()
	{
		$id = I('id');
        $map = ['s_id'=> $id];
        list($list, $page) = $this->lists('ShopReply', $map, 'id desc');
        if($list){
        	foreach ($list as $key => &$row) {
        		$row['content'] = $row['content'] ? base64_decode($row['content']):'';
        	}
        }
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
	}

	public function pinluan_del()
	{
        $id = I('id');
        $res = M('ShopReply')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }		
	}
}